package com.yzd.learnjava.io;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import com.yzd.learnjava.util.Print;

public class Blips {
	public static void main(String[] args) throws IOException, IOException, ClassNotFoundException {
		Print.print("Constructing objects");
		Blip1 b1 = new Blip1();
		Blip2 b2 = new Blip2();
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Blips.out"));
		
		Print.print("Saving objects");
		out.writeObject(b1);
		out.writeObject(b2);
		out.close();
		
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blips.out"));
		Print.print("Recovering objects");
		b1 = (Blip1)in.readObject();
		b2 = (Blip2)in.readObject();
		
	}
}

class Blip1 implements Externalizable{
	public Blip1() {
		Print.print("Blip1 constructor");
	}
	
	public void writeExternal(ObjectOutput out) {
		Print.print("Blip1 writeExternal");
	}
	
	public void readExternal(ObjectInput in) {
		Print.print("Blip1 readExternal");
	}
}

class Blip2 implements Externalizable{
	public Blip2() {
		Print.print("Blip2 constructor");
	}
	
	public void writeExternal(ObjectOutput out) {
		Print.print("Blip2 writeExternal");
	}
	
	public void readExternal(ObjectInput in) {
		Print.print("Blip2 readExternal");
	}
}
